
use chrono::{DateTime, Local, TimeZone, NaiveDateTime};

/**
 * 获取当前时间
 */
pub fn get_now_time(){
    let now: DateTime<Local> = Local::now();
    println!("当前日期和时间: {}",date_format(now) );
}

/**
 * 获取当前的毫秒
 */
pub fn get_now_millis() -> u32{
    let now: DateTime<Local> = Local::now();
    let millis = now.timestamp_subsec_millis();
    return millis;
}


/**
 * 获取当前的时间戳
 */
pub fn get_now_stamp() -> i64{
    let now: DateTime<Local> = Local::now();
    let millis = now.timestamp();
    return millis;
}


/**
 * 获取13位的毫秒
 */
pub fn get_stamps(date:DateTime<Local>) -> u32{
    let millis = date.timestamp_subsec_millis() * 1000 + date.timestamp_subsec_millis();
    return millis;
}

/**
 * 时间戳转日期
 */
pub fn stamp_date(stamp:u64) -> DateTime<Local>{
    let mut s = stamp;
    if stamp < 100000000000 {
        s = stamp * 1000000;
    }else if stamp < 100000000000000 {
        s = stamp * 1000;
    }
    return  Local.timestamp_micros(s as i64).unwrap();
}


/**
 * 格式转换
 */
pub fn date_format(date:DateTime<Local>) -> String{
    date.format("%Y-%m-%d %H:%M:%S").to_string()
}

/**
 * str 转换 日期类
 */
pub fn str_to_date(content:&str) -> DateTime<Local>{

        match NaiveDateTime::parse_from_str(content, "%Y-%m-%d %H:%M:%S") {
            Ok(date_time) => {
              return  date_time.and_local_timezone(Local).unwrap()
            }
            Err(_) => {Local::now()}
        }
    
}







